﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HumanManagement.BusinessLogic;
using HumanManagement.BusinessObjects;
using HumanManagement.DataAccess;
using System.Data;

namespace BSC_HumanManagement.Usercontrols
{
    public partial class uc_QuanHeGiaDinh_Detail : System.Web.UI.UserControl
    {
        int itemID = 0;
        FamilyRelationship objFamilyRelationship = new FamilyRelationship();
        FamilyRelationshipBL objFamilyRelationshipBL = new FamilyRelationshipBL();
        //public bool permissionAddNew = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtEffectiveDate.MaxDate = DateTime.MaxValue;
            this.txtEffectiveDate.MinDate = DateTime.MinValue;
            this.txtExpireDate.MaxDate = DateTime.MaxValue;
            this.txtExpireDate.MinDate = DateTime.MinValue;
            this.txtDob.MaxDate = DateTime.MaxValue;
            this.txtDob.MinDate = DateTime.MinValue;
            PersonalProfileBL _objPersonalProfileBL = new PersonalProfileBL();
            foreach (DataRow item in _objPersonalProfileBL.GetDataSet(Convert.ToInt32(Session["PersonalProfileID"].ToString())).Tables[0].Rows)
            {
                this.ddlPersonalProfileID.Items.Add(new ListItem(item["FullName"].ToString(), item["ID"].ToString()));
            }
            FamilyRelationshipCategoryBL objFamilyRelationshipCategoryBL = new FamilyRelationshipCategoryBL();
            foreach (DataRow item in objFamilyRelationshipCategoryBL.GetDataSet().Tables[0].Rows)
            {
                this.ddlFamilyRelationshipCategoryID.Items.Add(new ListItem(item["FamilyRelationshipCategory"].ToString(), item["ID"].ToString()));
            }
            OccupationBL objOccupationBL = new OccupationBL();
            foreach (DataRow item in objOccupationBL.GetDataSet().Tables[0].Rows)
            {
                this.ddlOccupationID.Items.Add(new ListItem(item["OccupationName"].ToString(), item["ID"].ToString()));
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Quan hệ gia đình Detail")
            {
                LoadData();
                DisableControl();
                ShowData();
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Quan hệ gia đình Edit")
            {
                if (LoadData())
                {
                    ShowData();
                    this.btnDelete.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hồ sơ nhân viên mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Quan hệ gia đình Delete")
            {
                if (LoadData())
                {
                    DisableControl();
                    ShowData();
                    this.btnSave.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hợp đồng mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
        }

        private void ShowData()
        {
            this.txtAddress.Text = objFamilyRelationship.Address;
            this.txtFullName.Text = objFamilyRelationship.FullName;
            this.txtIdentificationNumber.Text = objFamilyRelationship.IdentificationNumber;
            for (int i = 0; i < this.ddlPersonalProfileID.Items.Count; i++)
            {
                if (this.ddlPersonalProfileID.Items[i].Value.Equals(objFamilyRelationship.PersonalProfileID.ToString()))
                {
                    this.ddlPersonalProfileID.SelectedIndex = i;
                }
            }
            for (int i = 0; i < this.ddlFamilyRelationshipCategoryID.Items.Count; i++)
            {
                if (this.ddlFamilyRelationshipCategoryID.Items[i].Value.Equals(objFamilyRelationship.FamilyRelationshipCategoryID.ToString()))
                {
                    this.ddlFamilyRelationshipCategoryID.SelectedIndex = i;
                }
            }
            for (int i = 0; i < this.ddlOccupationID.Items.Count; i++)
            {
                if (this.ddlOccupationID.Items[i].Value.Equals(objFamilyRelationship.OccupationID.ToString()))
                {
                    this.ddlOccupationID.SelectedIndex = i;
                }
            }
            this.txtDob.SelectedDate = objFamilyRelationship.DateOfBirth;
            this.txtEffectiveDate.SelectedDate = objFamilyRelationship.EffectiveDate;
            this.txtExpireDate.SelectedDate = objFamilyRelationship.ExpireDate;
            for (int i = 0; i < this.ddlFamilyRelationshipStatus.Items.Count; i++)
            {
                if (this.ddlFamilyRelationshipStatus.Items[i].Value == objFamilyRelationship.FamilyRelationshipStatus)
                {
                    this.ddlFamilyRelationshipStatus.SelectedIndex = i;
                }
            }
        }

        private void DisableControl()
        {
            this.txtAddress.Enabled = false;
            this.txtDob.Enabled = false;
            this.txtEffectiveDate.Enabled = false;
            this.txtExpireDate.Enabled = false;
            this.txtFullName.Enabled = false;
            this.txtIdentificationNumber.Enabled = false;
            this.ddlFamilyRelationshipCategoryID.Enabled = false;
            this.ddlFamilyRelationshipStatus.Enabled = false;
            this.ddlOccupationID.Enabled = false;
            this.ddlPersonalProfileID.Enabled = false;
        }

        private bool LoadData()
        {
            itemID = Convert.ToInt32(Request.QueryString["ID"]);
            this.objFamilyRelationship = objFamilyRelationshipBL.GetByID(itemID, Convert.ToInt32(Session["PersonalProfileID"].ToString()));
            if (objFamilyRelationship == null)
            {
                return false;
            }
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.objFamilyRelationship.PersonalProfileID = Convert.ToInt32(this.ddlPersonalProfileID.SelectedValue);
            this.objFamilyRelationship.Address = txtAddress.Text;
            if (txtDob.SelectedDate.HasValue)
            {
                this.objFamilyRelationship.DateOfBirth = (DateTime)this.txtDob.SelectedDate;
            }
            else
            {
                this.objFamilyRelationship.DateOfBirth = DateTime.MaxValue;
            }
            this.objFamilyRelationship.FullName = txtFullName.Text;
            this.objFamilyRelationship.IdentificationNumber = txtIdentificationNumber.Text;
            this.objFamilyRelationship.OccupationID = int.Parse( ddlOccupationID.SelectedValue);
            this.objFamilyRelationship.FamilyRelationshipCategoryID = Convert.ToInt32(this.ddlFamilyRelationshipCategoryID.SelectedValue);

            if (txtEffectiveDate.SelectedDate.HasValue)
            {
                this.objFamilyRelationship.EffectiveDate = (DateTime)this.txtEffectiveDate.SelectedDate;    
            }
            else
            {
                this.objFamilyRelationship.EffectiveDate = DateTime.MaxValue;
            }

            if (txtExpireDate.SelectedDate.HasValue)
            {
                this.objFamilyRelationship.ExpireDate = (DateTime)this.txtExpireDate.SelectedDate;
            }
            else
            {
                this.objFamilyRelationship.ExpireDate = DateTime.MaxValue;
            }
            
            this.objFamilyRelationship.FamilyRelationshipStatus = this.ddlFamilyRelationshipStatus.SelectedValue;
            this.objFamilyRelationship.LastDate = DateTime.Now;

            if (Request.QueryString["ID"] == null)
            {
                objFamilyRelationshipBL.Add(objFamilyRelationship);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Tạo mới thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Quan hệ gia đình");
            }

            if (Request.QueryString["FunctionName"] == "Quan hệ gia đình Edit")
            {
                objFamilyRelationshipBL.Update(objFamilyRelationship);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Sửa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Quan hệ gia đình");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                objFamilyRelationship = objFamilyRelationshipBL.GetByID(Convert.ToInt32(Request.QueryString["ID"]), Convert.ToInt32(Session["PersonalProfileID"].ToString()));
                objFamilyRelationship.FamilyRelationshipStatus = "0";
                objFamilyRelationshipBL.Update(objFamilyRelationship);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Xóa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Quan hệ gia đình");
            }
            else { }
        }
    }
}